﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using AgencyManagerSystem.Model;
using AgencyManagerSystem.DataUpdater;
using AgencyManagerSystem.Common;
using AgencyManagerSystem.DataFetcher;

namespace AgencyManagerSystem.Core
{
    public class BillUpdaterCreator : MainDetailUpdaterCreator<BillModel, BillDetailModel>
    {
        BillFetcher billFetcher = new BillFetcher();
        BillDetailFetcher billDetailFetcher = new BillDetailFetcher();
        public BillUpdater CreateBillUpdaterInstance()
        {
            var mainUpdater = BillUpdater.GetMainUpdater();
            mainUpdater.DataUpdating += FlagAndApproveFlagValid;
            mainUpdater.DataUpdating += ReferExist;
            mainUpdater.DataUpdating += DateValid;
            mainUpdater.DataUpdating += PeriodUniqueValid;
            BillUpdater billUpdater = new BillUpdater(billFetcher, billDetailFetcher, mainUpdater);
            billUpdater.DataBulkUpdating += CommonValid;
            billUpdater.DataBulkUpdating += OrderDetailValid;
            billUpdater.DataBulkUpdating += AmountValid;
            return billUpdater;
        }

        private void AmountValid(MainDetailUpdater<BillModel, BillDetailModel> sender, DataBulkUpdatingEventArgs<BillModel, BillDetailModel> e)
        {
            if (e.Insert == null) return;
            if (e.DetailInsert == null) return;
            var amountsForSales = from item in e.DetailInsert where item.Qty>0 select item.Amount;
            var amountsForReturn = from item in e.DetailInsert where item.Qty < 0 select item.Amount;
            if(amountsForReturn.Sum()!=e.Insert.AmountForReturn
                ||Math.Abs(amountsForSales.Sum())!=e.Insert.AmountForSale
                ||e.Insert.AmountForSale+e.Insert.AmountForReturn!=e.Insert.Amount)
            {
                e.Cancel = true;
                e.Message = String.Format("经销商{0}在对账周期{1}/{2}的金额与明细档案不匹配", e.Insert.Retail, e.Insert.Year, e.Insert.Month);
                return;
            }
            if(e.Insert.Balance!=e.Insert.Amount - e.Insert.PrePay - e.Insert.Other)
            {
                e.Cancel = true;
                e.Message = String.Format("经销商{0}在对账周期{1}/{2}的余额校验失败", e.Insert.Retail, e.Insert.Year, e.Insert.Month);
                return;
            }
        }

        private void PeriodUniqueValid(BaseUpdater<BillModel> sender, DataUpdatingEventArgs<BillModel> e)
        {
            if (e.Insert == null) return;
            BillModel billModel = billFetcher.GetByRetailAndPeriod(e.Insert.Retail,e.Insert.Year,e.Insert.Month);
            if(billModel!=null&&billModel.AutoId!=e.Insert.AutoId)
            {
                e.Cancel = true;
                e.Message = String.Format("经销商{0}在对账周期{1}/{2}已经存在当期对账记录",e.Insert.Retail,e.Insert.Year,e.Insert.Month);
                return;
            }
        }
        

        private void OrderDetailValid(MainDetailUpdater<BillModel, BillDetailModel> sender, DataBulkUpdatingEventArgs<BillModel, BillDetailModel> e)
        {
            if (e.DetailInsert == null)
                return;
            foreach(var item in e.DetailInsert)
            {

                var orderDetailFetcher = new OrderDetailFetcher();
                var detail = orderDetailFetcher.Get(item.OrderDetail);
                if (detail == null)
                {
                    e.Cancel = true;
                    e.Message = String.Format("经销商{0}在对账周期{1}/{2}明细记录异常:{3}", e.Insert.Retail, e.Insert.Year, e.Insert.Month,item.OrderDetail);
                    return;
                }
                if(item.Amount!= item.Qty* item.Price)
                {
                    e.Cancel = true;
                    e.Message = String.Format("经销商{0}在对账周期{1}/{2}明细记录异常:{3}", e.Insert.Retail, e.Insert.Year, e.Insert.Month, item.OrderDetail);
                    return;
                }
            }
        }

        private void DateValid(BaseUpdater<BillModel> sender, DataUpdatingEventArgs<BillModel> e)
        {
            if (e.Insert == null)
                return;
            DateTime dt1, dt2;
            dt1 = dt2 = DateTime.Now;
            DateTime.TryParse(e.Insert.OpenDate, out dt1);
            DateTime.TryParse(e.Insert.DueDate, out dt2);
            if(dt1==dt2)
            {
                e.Cancel = true;
                e.Message = String.Format("经销商{0}在对账周期{1}/{2}校验异常，日期对账期间错误", e.Insert.Retail, e.Insert.Year, e.Insert.Month);
                return;
            }
        }

        private void ReferExist(BaseUpdater<BillModel> sender, DataUpdatingEventArgs<BillModel> e)
        {
            if (e.Insert == null)
                return;
            var retailFetcher = new RetailFetcher();
            var retail = retailFetcher.Get(e.Insert.Retail);
            if(retail==null)
            {
                e.Cancel = true;
                e.Message = String.Format("经销商{0}在对账周期{1}/{2}校验异常，经销商不存在", e.Insert.Retail, e.Insert.Year, e.Insert.Month);
                return;
            }
        }
    }
}
